package com.tx.review.exception_;

/**
 * 测试finally的demo
 *
 * @author ：tx
 * @version:
 */
public class FinallyDemo {
    public static void main(String[] args) {
//        int result = testFinally();
        int result = testFinallyReturn();
        System.out.println("==>" + result);
    }

    /**
     * finally在try和catch的return返回之前执行，如果finally也有return，那么调用者得到的就是finally的return值
    */
    private static int testFinally() {
        try {
            System.out.println("try block");
            int i = 10/0;
            return 1;
        }catch (Exception e){
            System.out.println("catch block");
            return 2;
        }finally {
            System.out.println("finally block");
            return 3;
        }
    }

    /**
     * 在catch中用了return就已经被标记了，在finally中只是做一些清理工作，并不会对catch中的值产生影响
    */
    private static int testFinallyReturn() {
        int j = 0;
        try {
            System.out.println("try block");
            int i = 10/0;
        }catch (Exception e){
            System.out.println("catch block");
            j=1;
            return j;
        }finally {
            System.out.println("finally block");
            j = 2 ;
//            return j;
        }
        j = 3;
        return j;
    }
}
